/**
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "tools/es2panda/server/language_server/panda_gen_options/generated/es2pandad_options.h"
#include "Endpoint.h"
#include "Es2pandad.h"
#include "utils/logger.h"
#include <memory>

namespace panda::es2panda::ls {

static int Run(int argc, const char **argv) {
    auto parser = std::make_unique<PandArgParser>();
    auto options = std::make_unique<Options>("");

    options->AddOptions(parser.get());

    if (!parser->Parse(argc,argv)) {
        std::cerr << parser->GetErrorString() << std::endl;
        std::cerr << parser->GetHelpString() << std::endl;
        return 1;
    }

    if (auto err = options->Validate()) {
        std::cerr << err.value().GetMessage() << std::endl;
        return 1;
    }

    panda::Logger::ComponentMask mask {};
    mask.set(Logger::Component::ES2PANDAD);
    Logger::InitializeStdLogging(Logger::LevelFromString(options->GetLogLevel()), mask);

    Endpoint ep(std::cin, std::cout);
    Es2pandaLSPServer svr(ep);
    svr.Run();

    return 0;
}

}

int main(int argc, const char **argv) {
    return panda::es2panda::ls::Run(argc, argv);
}
